package com.education.teacher.controller;

import com.education.util.ExcelUtil;
import org.springframework.web.bind.annotation.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.education.model.ResultDo;
import com.education.model.QuestionExam;
import com.education.service.SelectExamApi;
import com.education.util.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Map;

@Api(value = "/api/teacher", description = "题型:选择题的相关业务")
@RestController
@RequestMapping("/api/teacher")
public class SelectExamController {
    
    @Reference
    private SelectExamApi sea;
    
    

    /**
     * 2017/8/5 王娇
     * 选择题录入
     * 分页
     * 章节测试页面数据包括下拉框等控制层
     * @param pageNo
     * @param pageSize
     * @param searchMessage
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/page", httpMethod = "GET", value = "默认页面数据")
    @RequestMapping(value="/selectSection/page",method=RequestMethod.GET)
    public ResultDo<Object> getPage(@RequestParam("pageNum") Integer pageNo, Integer pageSize, String searchMessage) throws Exception{

        Map<Object,Object> map = sea.getPage(pageNo,pageSize,searchMessage,1);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        result.setResData(map);
        return result;
    }
    /**添加章节测试试题
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/addCourse", httpMethod = "POST", value = "添加章节测试试题")
    @RequestMapping(value="/selectSection/addCourse",method=RequestMethod.POST)
    public ResultDo<Object> addSelectSection(@RequestBody QuestionExam se,HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        se.setTeacherId(tid);
        sea.addCourseSection(se);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
    }


    /**添加期末测试试题se
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/addExam", httpMethod = "POST", value = "添加期末测试试题")
    @RequestMapping(value="/selectSection/addExam",method=RequestMethod.POST)
    public ResultDo<Object> addSelectExam(@RequestBody QuestionExam se,HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        se.setTeacherId(tid);
        sea.addExamService(se);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
    }
    /**添加入学测试试题
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/addEnter", httpMethod = "POST", value = "添加入学测试试题")
    @RequestMapping(value="/selectSection/addEnter",method=RequestMethod.POST)
    public ResultDo<Object> addSelectEnter(@RequestBody QuestionExam se,HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        se.setTeacherId(tid);
        sea.addEnterText(se);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
    }
    /**更新选择题
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/update", httpMethod = "POST", value = "更新选择题")
    @RequestMapping(value="/selectSection/update",method=RequestMethod.POST)
    public ResultDo<Object> updateSelectExam(@RequestBody QuestionExam se) throws Exception{

        sea.update(se);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
    }
    /**删除判断题
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/delete", httpMethod = "POST", value = "根据id查找判断题")
    @RequestMapping(value="/selectSection/delete",method=RequestMethod.POST)
    public ResultDo<Object> deleteJudgeCourseSection(@RequestBody QuestionExam se) throws Exception{
        sea.delete(se.getQuestionId());
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;

    }

    /**
     *2017/8/8 王娇
     * 批量录入章节选择题
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/selectSection/batchUpdate",httpMethod = "POST",value = "批量录入")
    @RequestMapping(value = "/selectSection/batchUpdate",method = RequestMethod.POST)
    public ResultDo<Object> batchAddSection(MultipartFile excelFile,QuestionExam questionExam, HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        questionExam.setTeacherId(tid);
        Map<String, ArrayList<String>> map= ExcelUtil.readExcelData(excelFile.getInputStream());

        sea.excelUplode(map,questionExam);
        ResultDo<Object> resultDo = new ResultDo<Object>();

        resultDo.setResCode(0);
        resultDo.setResMsg("导入成功");
        return resultDo;
    }

    
    
}
